import React, {PropsWithChildren, useState} from 'react'
import MyNavigationBar from "../components/MyNavigationBar";
import {Button, Form, Input} from "@nutui/nutui-react-taro";
import {updatePassword} from "../apis/AuthorizationApi";
import Taro from "@tarojs/taro";
import styles from "./updatePassword.module.sass"

export interface UpdatePasswordProps extends PropsWithChildren {

}

const UpdatePassword: React.FC<UpdatePasswordProps> = ({}) => {
    const [loading, setLoading] = useState(false)
    return <>
        <MyNavigationBar>修改密码</MyNavigationBar>
        <Form className={styles.container}
              footer={<Button block type={"primary"}
                              formType={"submit"}
                              loading={loading}>确定</Button>}
              labelPosition={"top"} initialValues={{}}
              onFinish={async (values) => {
                  setLoading(true)
                  await updatePassword({
                      password: values.oldPassword,
                      newPassword: values.newPassword,
                  })
                  await Taro.showToast({
                      title: "修改成功",
                      icon: "success"
                  })
                  Taro.reLaunch({
                      url: "/pages/logIn"
                  })
                  setLoading(false)
              }}
        >
            <Form.Item label={"旧密码"} name={"oldPassword"} rules={[{
                required: true,
                message:"请输入当前密码"
            }]}>
                <Input placeholder={"请输入当前密码"} clearable type={"password"}/>
            </Form.Item>
            <Form.Item label={"新密码"} name={"newPassword"} rules={[{
                required: true,
                message:"请输入新密码"
            }]}>
                <Input placeholder={"请输入新密码"} clearable type={"password"}/>
            </Form.Item>
        </Form>
    </>
}

export default UpdatePassword


